{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import time\n",
    "import random\n",
    "from tqdm import tqdm\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import numpy as np\n",
    "from PIL import Image, ImageDraw, ImageFont\n",
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n"
     ]
    }
   ],
   "source": [
    "vercode_url = 'http://satellite.nsmc.org.cn/PortalSite/Sup/user/LoginGenCodeImg.aspx'\n",
    "r = requests.get(vercode_url)\n",
    "print(r.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# with open('images/vercode_test.gif', 'wb') as f:\n",
    "#     f.write(r.content)\n",
    "\n",
    "# for i in tqdm(range(0, 100)):\n",
    "#     r = requests.get(vercode_url)\n",
    "#     if r.status_code == 200:\n",
    "#         with open('images/vercode_test/vc_{:0>3d}.gif'.format(i), 'wb') as f:\n",
    "#             f.write(r.content)\n",
    "#     time.sleep(0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASwAAACECAIAAAClJ4EVAAAABnRSTlMAZgAAAAB41IV8AAAJQUlEQVR4nO2dwW3DRhBFqUDVyL5RrdglUGVYLoMsQWqFvNluRzkG2TcAJ4zkUYL3josld5figMDXn5ndfJm7hpd2oPvGiHOc45w7zfkDU0TkVzEIRYoxCEWKMQhFijEIRYrZzd+tOtof+pKt/Bum69SM9C/tKZbv5anm8Dkfd7tmZL7dul/jvR2Y3tqnOrwND1p8+mnXWs7tMxwv44NW/02Wa3suv4QixRiEIsUYhCLFGIQixRiEIsXsqDjdSwGjYpm58+61ver21V51hL7UX9v7jJdn13gnniLhPFxecNU3tNk7zck4I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DLERf125yimzghr39MuAojzbCF3aZ4irm1qebAr7ygbxSfa+bNyFTaDBRm5qTzLrd2h+MnuzJtgl7fhJZONzI5vrY7nL/W6x74JRQpxiAUKcYgFCnGIBQpxiAUKSboWb+tgugDubb2ROakM7d9G4HHkpUHMv2MMv3fN87BDlHRtA/qjmIE2id7XeV6OTGTPXEfwP2k7kN3a+nbe8K7OqL3Ft8xv4QixRiEIsUYhCLFGIQixRiEIsXs5m/0RaLiBFXqiJGZOh5HEkwv6MAOLXRC13juMFp9vWc9ewPdiwmZ7FEt0N+D9Tn5u2c6HN2LTBf7u2mzCc056KKFN7Pf5I7mk/dLKFKMQShSjEEoUoxBKFKMQShSzH/BO/o/JegflOiKHnRyZ//3b1RvTal/63O2uWQX9EXqoUvXarMTayMkTpHRt6cdCrMi4vwSihRjEIoUYxCKFGMQihRjEIoUs5uhpPWH+3gal892pGf/7icjUsnWNcMJc4ZAM4TWl8h2n3DVnHC38hQDezBd2P9K/mJ6bVXN4etO1Rv0joo8GwahSDEGoUgxBqFIMQahSDGBdzTKRKaDETnpmBN049nkKszU57zXfXhSqpqZygNRDjiuSqx1rz3n9sO17uM43brnbb/Foxyw2+rEZtbySyhSjEEoUoxBKFKMQShSjEEoUsyfbPNzyOjnrRQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=300x132 at 0x7F88D33F68D0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img = Image.open('images/vercode/vc_1600.gif')\n",
    "# img = Image.fromarray(cv2.GaussianBlur(np.array(img), (3, 3), 0))\n",
    "img = img.resize((img.size[0]*6, img.size[1]*6), resample=Image.BICUBIC).convert('RGB')\n",
    "img"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASwAAACECAAAAAAPLkmeAAAER0lEQVR4nO2byXLcMAxEpZ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utX7LWLDpg/CZSLLzl1twvcSv9b2VWbGKtvOvApMsOqcUrHyfWC8/feTz8qa/7JEOWruqGlgk14pz2p2i1vUTa2vZ2p3EtdOmdwJ9wgCA8w95mA7wpsS5FLPmCs5VYARe2rWpB5Ta6zEbcwBwVq3+SAdTENN0ns6J6TGM4nWYs6HsbsWbx+tajzmOhb6K64e4X5M++2njoyzJr9GuvNxx+PiY8G8WaXJ1227OUYAseBzv/WjML+gUDbCEwr/qmGt+QfhcO8J9Iseb1nxxX3YFtI3ubem+8/tgwXajsgc+p3s7A4fNr9bl01nU4/6i1+7IhDM+DFrcFIYQQQgghhBBCCCGEEEIIIYQQQgiO3x4u76vtQnZGAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<PIL.Image.Image image mode=L size=300x132 at 0x7F88D3400B70>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img_pro = img.convert('L')\n",
    "img_pro = cv2.GaussianBlur(np.array(img_pro), (3, 3), 0)\n",
    "img_pro = cv2.medianBlur(img_pro, 11)\n",
    "ret, img_pro = cv2.threshold(img_pro, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)\n",
    "img_thresh = img_pro.copy()\n",
    "Image.fromarray(img_pro)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# kernel = np.ones((3,3),np.uint8)\n",
    "# img_pro = cv2.morphologyEx(img_pro, cv2.MORPH_ERODE, kernel, iterations=2)\n",
    "# Image.fromarray(img_pro)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASwAAACECAAAAAAPLkmeAAAFLElEQVR4nO1c27IcIQjUVP7/l83DnnPiKGCD6G4l3Q9bc1FQRAR0thSCIAiCIAiCIAiCIAiCIAiCIAiCIAiCIAjin0R1lm/eCkdIdMT6G5xw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nppXybFQEANFc9lUb/HbIK5MC9NguwR5vu0tqYKCUQvirxI0cZI98R+QqE1EqrNlpTdG8xC7a4gEMtC5sW449J0NgkRoTld0rN4wrQgrw1iMoBR2wSbk61gAdvcITabB8rXhAQOQR2tUKIhDvGiT0s/thRLTEBg4lq24bn7u4cH1+5v7eiv0xhwSOXe8Lyyr5sKeWTvwqTcE8uIhKF5ejJnc+GF1z9bcgT1oX+KxYL5/wMcNxqmqhZHt77h3ehFyPX2v/5gX9Kh/JZUiB1w5yocV0z30tFL+ZPoRyll8KKSiR3CrcKKpg1DXMUa9Pu9dMsh+ITn+Q62FELNBxj4FrBz1UwmX7ON9I5SfE6fNJaSwFO+R1NyIObKB4K/gSro+CCOPqx1OdMw5PLk/0VzOGNnn3N8m59xRzP/rGaKjnuTe8vOs6tr30nQMk73wg8vH0VSPjqoxxcf6zlbHfQZs2fxvnRuivE9waJ6qQEl/WSH7+/Q+n8C0GomFlo3J10NzvjXH54gJqrCS82ix7axPrKV1NjTbg6zTKgDZ8B+Q9K/mlsKOP9ZAdBEARBEARBEARBEARBEARBEARBEARBEP89/gDxUX4zs76dkQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<PIL.Image.Image image mode=L size=300x132 at 0x7F88D34007F0>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img_pro = cv2.Canny(img_pro, 80, 200, L2gradient=True)\n",
    "Image.fromarray(img_pro)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASwAAACECAIAAAClJ4EVAAATi0lEQVR4nO2d3bWbOhCFh7tONcd5E63YJUhlxKcMqQTcCrzFaYf7YBsP2gIEBiQ78608JA7mV9sjtkajoq5q8jj4H9AVPpFtZBvZZqVt/oNNBEHYFRGhICRGRCgIiRERCkJiRISCkJiivvruqPpWSU7lFdzFeZ+og38VzbXJahu8z2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QkFIjIhQEBIjIhSExIgIBSExIkJBSMz/cwjv6lohRYkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=300x132 at 0x7F88D340F630>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 再做轮廓检测\n",
    "cnts = cv2.findContours(img_pro.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]\n",
    "# 可过滤一些非常小的轮廓，轮廓小于16的过滤，！注意：这个参数应当随图像尺寸大小变化面变化\n",
    "cnts_filter = [c for c in cnts if len(c) >= 50]\n",
    "roi_sample_gray_cnts = cv2.drawContours(np.array(img), cnts, -1, (255, 0, 0), 2)\n",
    "Image.fromarray(roi_sample_gray_cnts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASwAAACECAIAAAClJ4EVAAATgUlEQVR4nO2dUZabOgyGxT1dzaRvZivJEswymlmGvYRkK/DWme1wH5gQ2b8BQwCbVN/pQ5sSsMF/ZGRJLupbTR4n/wP6gk/kGDlGjlnpmP/gEEEQdkVEKAiJEREKQmJEhIKQGBGhICSmqL9876j6UEma8gr2br1P1MnvRfPVZHUM3ueyKLxP6ral3bj4H9izf1f1WW90cfvtX6u5+vfQ3MxGV9+T5u73SyyhICRGRCgIiRERCkJiRISCkBgRoSAkpkCP01oeMPRYxpy5+O1/q/3rf6sE/5K6++cxt9x9vBZ7ERF52JzgW1/gm13pmJhIyAaOUeAZbj7BM/wHzoxEXF2dc3/KSPMt3lFByAwRoSAkRkQoCIkREQpCYkSEgpCYDWNHq08/EtL82S8SsriU3ict1hBYiQriHs3HVjGWVQHXapdcCyMY0RsZEwGLHssSjmk/5raOKM673mDEKfh4FfiBVzvmo/KPuYOawH+L/RJLKAiJEREKQmJEhIKQGBGhICRGRCgIiSkCdUeBBiP0wCu1LPI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USHT9Yo0lgCO/Tz0gx4cY+C+oNyPlDd0XFuzHF/GZ377/t6Xd/raRsozMR8m+ojx/RLP0x/JdKL0J9yIJ1li+3+a+6Z+Va0vteBOj0RVbHxFzHuGLDVMJdRgQrczj+rLH84zLfhf1c4R4u4P/vkAK1LehEK+2PuNW5VqXGGETHocQIZJR4A2/PvkGydcCPLvg9dMqEgrEJKSxirw7s/w8T0U7WSR3TaMROcagrCCxQ1lHPG3dXxHaOET2ocmosGqz1ByDxIzn7gvuQxV8c9dOBtCvYPWgsLJYB10uBprM+Jzz1mh6O1CO1wBAetNRmOeNMOrBrAyFSLVgTwzucdtiYI/wAiQkFIjIhQEBIjIhSExIgIBSExyeqOCoH9g3Bv98CSCS5qw67xX5AaHuX9mz5mWWxQA/siKfBLp/XNWtw3KqIXMf5tizlAoDixhIKQGBGhICRGRCgIiRERCkJiRISCkJiiBk+a+lgnprH59D9RK9Qj2JaQl2zaZ2jhGB3wGYKvDyMhIbrVwrfqiOhW7IWGyFW67b6/yqGwv32vpv67UpKAxI4KQm6ICAUhMSJCQUiMiFAQEiMiFITEBGJHQ5nIGMEIOelwTGA3nkVRhTFVydY6D/YUvZoxlQdCOeDwrYhrrdXmuPbgtdaJOF3a5mXPYqsI2GXV8WKuJZZQEBIjIhSExIgIBSExIkJBSIyIUBAS8z/0D6AZpHdTKQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=300x132 at 0x7F88D340F4E0>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rects = [cv2.boundingRect(cf) for cf in cnts_filter]\n",
    "roi_sample_rects = roi_sample_gray_cnts.copy()\n",
    "for x, y, w, h in rects:\n",
    "    cv2.rectangle(roi_sample_rects, (x, y), (x+w, y+h), (0, 255, 0), 2)\n",
    "\n",
    "Image.fromarray(roi_sample_rects)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_rect(rect, max_width=50):\n",
    "    rect_num = rect[2]//max_width\n",
    "    if rect_num<=1:\n",
    "        return [rect]\n",
    "    \n",
    "    rect_lst = []\n",
    "    rect_width = rect[2] // rect_num\n",
    "    for i in range(rect_num):\n",
    "        rect_temp = list(rect)\n",
    "        rect_temp[0] = rect_temp[0] + i*rect_width\n",
    "        rect_temp[2] = rect_width\n",
    "        rect_lst.append(tuple(rect_temp))\n",
    "    return rect_lst\n",
    "\n",
    "filter_rects = []\n",
    "for rt in rects:\n",
    "    # 矩形面积小于1500的过滤掉\n",
    "    if rt[2]*rt[3] <= 1500:\n",
    "        continue\n",
    "    # 将大的矩形框分割开\n",
    "    filter_rects.extend(split_rect(rt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w: 61, h: 76\n",
      "w: 61, h: 73\n",
      "w: 49, h: 74\n",
      "w: 49, h: 73\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASwAAACECAIAAAClJ4EVAAATgUlEQVR4nO2dUZabOgyGxT1dzaRvZivJEswymlmGvYRkK/DWme1wH5gQ2b8BQwCbVN/pQ5sSsMF/ZGRJLupbTR4n/wP6gk/kGDlGjlnpmP/gEEEQdkVEKAiJEREKQmJEhIKQGBGhICSmqL9876j6UEma8gr2br1P1MnvRfPVZHUM3ueyKLxP6ral3bj4H9izf1f1WW90cfvtX6u5+vfQ3MxGV9+T5u73SyyhICRGRCgIiRERCkJiRISCkBgRoSAkpkCP01oeMPRYxpy5+O1/q/3rf6sE/5K6++cxt9x9vBZ7ERF52JzgW1/gm13pmJhIyAaOUeAZbj7BM/wHzoxEXF2dc3/KSPMt3lFByAwRoSAkRkQoCIkREQpCYkSEgpCYDWNHq08/EtL82S8SsriU3ict1hBYiQriHs3HVjGWVQHXapdcCyMY0RsZEwGLHssSjmk/5raOKM673mDEKfh4FfiBVzv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gPXoIs5svOpng0KSWr1R1d6AuNwNxK7jA0t1K7UY7K/WeNVSWjwbg+v/JAn/j/QUSHT9Yo0lgCO/Tz0gx4cY+C+oNyPlDd0XFuzHF/GZ377/t6Xd/raRsozMR8m+ojx/RLP0x/JdKL0J9yIJ1li+3+a+6Z+Va0vteBOj0RVbHxFzHuGLDVMJdRgQrczj+rLH84zLfhf1c4R4u4P/vkAK1LehEK+2PuNW5VqXGGETHocQIZJR4A2/PvkGydcCPLvg9dMqEgrEJKSxirw7s/w8T0U7WSR3TaMROcagrCCxQ1lHPG3dXxHaOET2ocmosGqz1ByDxIzn7gvuQxV8c9dOBtCvYPWgsLJYB10uBprM+Jzz1mh6O1CO1wBAetNRmOeNMOrBrAyFSLVgTwzucdtiYI/wAiQkFIjIhQEBIjIhSExIgIBSExyeqOCoH9g3Bv98CSCS5qw67xX5AaHuX9mz5mWWxQA/siKfBLp/XNWtw3KqIXMf5tizlAoDixhIKQGBGhICRGRCgIiRERCkJiRISCkJiiBk+a+lgnprH59D9RK9Qj2JaQl2zaZ2jhGB3wGYKvDyMhIbrVwrfqiOhW7IWGyFW67b6/yqGwv32vpv67UpKAxI4KQm6ICAUhMSJCQUiMiFAQEiMiFITEBGJHQ5nIGMEIOelwTGA3nkVRhTFVydY6D/YUvZoxlQdCOeDwrYhrrdXmuPbgtdaJOF3a5mXPYqsI2GXV8WKuJZZQEBIjIhSExIgIBSExIkJBSIyIUBAS8z/0D6AZpHdTKQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=300x132 at 0x7F88D341F278>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "roi_sample_rects = roi_sample_gray_cnts.copy()\n",
    "# print(len(filter_rects))\n",
    "alpha_thresh_lst = []\n",
    "alpha_color_lst = []\n",
    "for x, y, w, h in sorted(filter_rects, key=lambda x: x[0]):\n",
    "    cv2.rectangle(roi_sample_rects, (x, y), (x+w, y+h), (0, 255, 0), 2)\n",
    "    alpha_thresh_lst.append(img_thresh[y:(y+h), x:(x+w)])\n",
    "    alpha_color_lst.append(np.array(img)[y:(y+h), x:(x+w)])\n",
    "    print('w:%3d, h:%3d'%(w, h))\n",
    "\n",
    "Image.fromarray(roi_sample_rects)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjAAAAFUCAYAAAA6fAa+AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy8QZhcZAAATqklEQVR4nO3dUVbjxhYF0HLWGw3kTz0VeghmGCHDgCHAVNBf6On4fdAknU4DPrYl1ZX2/kjWSoyRRanqVEmuuzscDg0AoJLflj4AAICUAAMAlCPAAADlCDAAQDkCDABQjgADAJTzv4/+52638x3rlTocDruljwGmoN/qR9V+Rhvqy3vt6MMAE7z5L//7bley7Zby87nv7ZzrCLah6kDVkyX35Oqt3+B8c7anpdrPRQIMXIowvKzeAzHAm7MCzGcJT2d4ec45rEsPu6HrN+pbsh29/e65240VGBZ17EWng52GQAxUdVKAOTXpHQ4HHeCJzjnnrRl4oBc9rLh8Rr9RR0/tae52YwWGRZx70elgzyMQA9VFAeYSSU8HmHPeoa6eZsgp/Ua/em5Xc7UbKzDM6tIXnQ42c6nz77wDSzsqwEyR9HSAwJr1PENO6a85xdTtxgoMbMCaBlOA1gSYrk258gVrtfRqgWuMKVVsX1NdkwIMs5j6olt60Nqqip0psA6LV6M+HA46QYAC9Nec49LtZ/EAAwCQcgupQ1POcN5usZhFsVaefWGNtK//EmBgxabu9ARiYCkCDMAZthjePDTPOS7VfgQYWKEtDqrAtggwHZnj2RfgPMLhP6zETG/N7e3c9iPAADEDFrA0AQbgCGueCdOfLbW3U1diBBhY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jj15tIwC2xAoMAFCOAAMAlKOUANO6eT7xB0/Zkns6w0v2+v1f2U292132/s/B7cVT5DVy+rmJ+Xbs49O0N6jHsE0ML/nxfAnbxRBeN2N48/n+kDwg4NY207ICAwCUYwUGAP5l+P7PsNDiIV9lG3dfstfHvyGTruK11toYVinfX2jF1goMAFCOAAMAlCPAAADlCDAAQDkCDABQzjTfQgqeMN6HTzynz3gPj7bjB4C1sQIDAJQjwAAA5QgwAEA5duJlUrffXv99P3FtnbwGTGZ4DF8fvv/zIfyBqV3HZ2iSwzjJ92Mfv2a7g6ZP2M1xhvKfSa+bTOUaWayPFRgAoBwrMADwg7eVpuGQreLd/5nXQrqNf2Jij/ly8P3NBMdxBCswAEA5AgwAUI4AAwCUI8AAAOUIMABAOYt/C+n+8Uv0+v1T9v7DTW8bbBwv2XNhvM7ee39lfwYA6rICAwCUI8AAAOUIMABAOYs/A8O6Pf8x7Ob4PfcHz/Rc0nCV1hHqx/jyvS08ZnV79i38zOFzZ+0lfH1rbQxrUg0v4XWQfgboiBUYAKCc3eFQ91s6AMA2WYEBAMoRYACAcgQYAKAcAQYAKEeAAQDKEWAAgHL+D5RX0jlgaR2XAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 720x432 with 8 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, axes = plt.subplots(2, 4, figsize=(10, 6))\n",
    "for ax, alpha in zip(axes.flatten(), alpha_thresh_lst+alpha_color_lst):\n",
    "    ax.imshow(alpha, cmap='gray')\n",
    "    ax.axis(False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPoAAABkCAIAAAAg4rpZAAAdHElEQVR4nO1dd1xUx/Y/C+wCCwQFRCwRbFgw9g7y/KgxPhvGrklMrFFjJJb4jMYWfcaXxGDsRJ9GjUZTbUGfGgMaEbB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ICAQGWqPJn7DK3FWo+O+EugOwCjuCau4q7AiquauwI6jmrsKOoJq7CjuCau4q7Aj/BwwoLqKQML2EAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=250x100 at 0x7F88C81D5E48>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# font_path = 'C:/Windows/Fonts/arialbi.ttf' #windows\n",
    "font_path = '/usr/share/fonts/truetype/ubuntu/Ubuntu-BI.ttf' #linux\n",
    "# make a blank image for the text, initialized to transparent text color\n",
    "txt = Image.new('RGB', (250, 100), (255, 255, 255))\n",
    "\n",
    "# get a font\n",
    "fnt = ImageFont.truetype(font_path, 40)\n",
    "# get a drawing context\n",
    "d = ImageDraw.Draw(txt)\n",
    "\n",
    "# draw text, half opacity\n",
    "d.text((10,5), \"Hello World\", font=fnt, fill=(255,0,0,128))\n",
    "# draw text, full opacity\n",
    "d.text((10,55), \"0123456789\", font=fnt, fill=(255,0,0,255))\n",
    "\n",
    "# out = Image.alpha_composite(base, txt)\n",
    "txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "36"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chr(random.randint(65, 90))\n",
    "len(\"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Z (23, 28)\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAHgAAAB4CAIAAAC2BqGFAAAJcElEQVR4nO2daWxVRRTHz5WnspTyZFEkBYQWZBEslM2FxbKkaAxlaQFBEFksGFBkM5qYiCHKooVgAMtmMYootRLcAIFiZVGLrbK4gLhQQUSghUorS68fMOb8zz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luM3X+2J6mlCIQjk8MxCiELRpQcOwUQZOKyKoBTDEJcjUnvn578OfLi4foDFwlWVJnUC19olOMRbj1aSGVpJZmglmaGVFPXRit5D4fhC9ynwSbJnIlwLRklOfTxx0LA9BOoeM/Jpjj1TkqH0rWIpnui7cpkSJGwt8vVocJXDADrOcQN+qV15GqCJgzhZj1aSGVpJZmgleb4fjyl4I80TssLaeos4Zq/bgPngqtTNubh4D66uZ8wCh89BI2ADVBxmu9qKtDkrlYhYJPOnIuKBAuvRSjJDK8kMrSSvL75Hbx4DV89X9FuK5Sn1Exy2owzPRiSimOFSTxQ3N17R/Ygbo30arhaPQYC2kQkQEiP5ebDMn+5r9CInhcl6tJLM0EoyQyvJDK0kz/fDnM+XQTjqtO4DRIXLnfEAwR5cSREROr51fhD2tSnpAuBecd4hGe+G2/aDaEzcjnDC+bWo5CXAz72dDd9iMbVhph5PMDcS0fKxcEiFVsLMbD1aSWZoJZmhlRQSt4wdKITN4cvtpTtpx8YwKH+J29wzcVA+NFwGWc5dC18oIvAQDcR7wcLO86KCB2Wx61CMeMKJdNqsGvgyiUFZXCa6XB6HI8KYSsLz1Xq0kszQSjJDKykkVrvbFHwI+YXzRYXGEFtUBEmmbbj/WbBW+uiPmQp+86sz0UUzN+hZq5KIJQT9Zi/eokw0AXEuIrVLgujV4n6D/WJQTkwV1e8oepdjBY7S1qOVZIZWkhlaSV7fRFz4L8QINmlOhGd5t+MkxCUc3Gtc9uNlwKGfITxP8i/wWg53Ulalm/Fd+bjzXn4V1QD3nU86+87Bsh6tJDO0kszQSjJDK8nzY/A9XMRuQ49KIqIkcKoU009/9LHMFw6WVexrj0JcU+VT/qPbnRQM6IRB9ygNT8uJWXwg9RSNncSDJVXcIxGoDoiFuGViPVpJZmglmaGV9DdoZRTUHV+yWQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=120x120 at 0x7F88C81D5E10>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits_letters = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n",
    "r_fontsize = random.randint(18, 32)\n",
    "r_px = np.random.randint(-6, 3)\n",
    "r_py = np.random.randint(-6, 3)\n",
    "r_dl = digits_letters[np.random.randint(0, 36)]\n",
    "# r_dl = 'W'\n",
    "\n",
    "fnt = ImageFont.truetype(font_path, r_fontsize)\n",
    "txt_size = fnt.getsize(r_dl)\n",
    "txt = Image.new('RGB', txt_size, (255, 255, 255))\n",
    "\n",
    "d = ImageDraw.Draw(txt)\n",
    "d.text((r_px, r_py), r_dl, font=fnt, fill=tuple(np.random.randint(0, 120, 3)))\n",
    "\n",
    "# 添加噪声\n",
    "mask = np.random.randint(0, 10, (txt_size[1], txt_size[0], 3))\n",
    "mask = np.where(mask<2, True, False)\n",
    "img_noise = mask*np.random.randint(0, 120, (txt_size[1], txt_size[0], 3), dtype=np.uint8)\n",
    "# Image.fromarray(np.array(txt) - img_noise)\n",
    "print(r_dl, txt_size)\n",
    "Image.fromarray(np.array(txt) + img_noise).resize((120, 120))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
